package com.yan.educenter.controller;


import com.yan.commonutils.JwtUtils;
import com.yan.commonutils.R;
import com.yan.educenter.entity.UcenterMember;
import com.yan.educenter.entity.Vo.RegisterVo;
import com.yan.educenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-29
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;
//    登录
    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
        String token=memberService.login(member);


        return R.ok().data("token",token);
    }

//    注册
    @ApiOperation(value = "注册")
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);

        return R.ok();
    }

    @ApiOperation(value = "根据用户id获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
//        调用jwt工具类方法，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
//        查询数据库，根据用户id获得用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);


    }



}

